"""
记账相关的数据库模型
"""

import enum
from datetime import datetime
from decimal import Decimal

from sqlalchemy import Boolean, DateTime, Enum, Integer, Numeric, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from src.database import Base


class TransactionType(enum.Enum):
    """交易类型枚举"""

    INCOME = "income"  # 收入
    EXPENSE = "expense"  # 支出
    TRANSFER = "transfer"  # 转账


class AccountType(enum.Enum):
    """账户类型枚举"""

    CASH = "cash"  # 现金
    BANK = "bank"  # 银行账户
    CREDIT_CARD = "credit_card"  # 信用卡
    ALIPAY = "alipay"  # 支付宝
    WECHAT = "wechat"  # 微信
    OTHER = "other"  # 其他


class Category(Base):
    """记账分类模型"""

    __tablename__ = "categories"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, comment="分类ID")
    name: Mapped[str] = mapped_column(String(50), nullable=False, comment="分类名称")
    parent_id: Mapped[int | None] = mapped_column(
        Integer, nullable=True, comment="父分类ID,用于二级分类"
    )
    parent_name: Mapped[str | None] = mapped_column(
        String(50), nullable=True, comment="父分类名称"
    )
    family_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="家庭ID")
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )
    is_system: Mapped[bool] = mapped_column(
        Boolean, default=False, comment="是否系统内置分类"
    )
    icon: Mapped[str | None] = mapped_column(
        String(50), nullable=True, comment="图标标识"
    )
    color: Mapped[str | None] = mapped_column(
        String(20), nullable=True, comment="颜色代码"
    )
    sort_order: Mapped[int] = mapped_column(Integer, default=0, comment="排序顺序")


class Account(Base):
    """账户模型"""

    __tablename__ = "accounts"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, comment="账户ID")
    name: Mapped[str] = mapped_column(String(50), nullable=False, comment="账户名称")
    account_type: Mapped[AccountType] = mapped_column(
        Enum(AccountType), nullable=False, comment="账户类型"
    )
    balance: Mapped[Decimal] = mapped_column(
        Numeric(10, 2), default=Decimal("0.00"), comment="账户余额"
    )
    initial_balance: Mapped[Decimal] = mapped_column(
        Numeric(10, 2), default=Decimal("0.00"), comment="初始余额"
    )
    family_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="家庭ID")
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, comment="是否启用")
    icon: Mapped[str | None] = mapped_column(
        String(50), nullable=True, comment="图标标识"
    )
    color: Mapped[str | None] = mapped_column(
        String(20), nullable=True, comment="颜色代码"
    )
    description: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="账户描述"
    )


class Project(Base):
    """项目模型"""

    __tablename__ = "projects"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, comment="项目ID")
    name: Mapped[str] = mapped_column(String(100), nullable=False, comment="项目名称")
    description: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="项目描述"
    )
    family_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="家庭ID")
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, comment="是否启用")
    start_date: Mapped[datetime | None] = mapped_column(
        DateTime, nullable=True, comment="开始日期"
    )
    end_date: Mapped[datetime | None] = mapped_column(
        DateTime, nullable=True, comment="结束日期"
    )
    budget: Mapped[Decimal | None] = mapped_column(
        Numeric(10, 2), nullable=True, comment="预算金额"
    )
    color: Mapped[str | None] = mapped_column(
        String(20), nullable=True, comment="颜色代码"
    )


class Transaction(Base):
    """交易记录模型"""

    __tablename__ = "transactions"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, comment="交易记录ID")
    transaction_type: Mapped[TransactionType] = mapped_column(
        Enum(TransactionType), nullable=False, comment="交易类型"
    )
    amount: Mapped[Decimal] = mapped_column(
        Numeric(10, 2), nullable=False, comment="交易金额"
    )
    category_id: Mapped[int | None] = mapped_column(
        Integer, nullable=True, comment="分类ID"
    )
    category_name: Mapped[str | None] = mapped_column(
        String(50), nullable=True, comment="分类名称"
    )
    account_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="账户ID")
    account_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="账户名称"
    )
    to_account_id: Mapped[int | None] = mapped_column(
        Integer, nullable=True, comment="目标账户ID(转账时使用)"
    )
    to_account_name: Mapped[str | None] = mapped_column(
        String(50), nullable=True, comment="目标账户名称"
    )
    project_id: Mapped[int | None] = mapped_column(
        Integer, nullable=True, comment="项目ID"
    )
    project_name: Mapped[str | None] = mapped_column(
        String(100), nullable=True, comment="项目名称"
    )
    member_id: Mapped[int] = mapped_column(
        Integer, nullable=False, comment="家庭成员ID"
    )
    member_name: Mapped[str] = mapped_column(
        String(100), nullable=False, comment="家庭成员姓名"
    )
    family_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="家庭ID")
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )
    transaction_date: Mapped[datetime] = mapped_column(
        DateTime, nullable=False, comment="交易日期"
    )
    description: Mapped[str | None] = mapped_column(
        Text, nullable=True, comment="交易描述"
    )
    is_recurring: Mapped[bool] = mapped_column(
        Boolean, default=False, comment="是否定期交易"
    )
    recurring_pattern: Mapped[str | None] = mapped_column(
        String(50), nullable=True, comment="定期模式"
    )
    tags: Mapped[str | None] = mapped_column(
        String(500), nullable=True, comment="标签,逗号分隔"
    )
    receipt_image: Mapped[str | None] = mapped_column(
        String(255), nullable=True, comment="收据图片URL"
    )
    is_deleted: Mapped[bool] = mapped_column(
        Boolean, default=False, comment="是否已删除"
    )
